package com.eol.common.oss;

import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.eol.common.utils.FileUtil;
import lombok.extern.slf4j.Slf4j;
import com.eol.common.config.SystemPropertiesConfig;
import com.eol.common.sequence.Sequence;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: liulinhui
 * Date: 2019-02-28
 * Time: 18:36
 * Description:  OssUtils
 */
@Configuration
@Slf4j
@Service
public class OssUtils {

    @Resource
    private Sequence sequence;
    @Resource
    private SystemPropertiesConfig systemPropertiesConfig;
    @Resource
    private OSSClient ossClient;

    @Bean
    public OSS ossClient() {
        return new OSSClientBuilder().build(systemPropertiesConfig.getAliYunOss().getEndpoint(),
                systemPropertiesConfig.getAliYunOss().getAccessKeyId(), systemPropertiesConfig.getAliYunOss().getAccessKeySecret());
    }

    public void uploadFile(String fileName, byte[] bytes) {
        ossClient.putObject(systemPropertiesConfig.getAliYunOss().getBucketName(), fileName, new ByteArrayInputStream(bytes));
    }

    public void uploadFile(String fileName, File file) {
        ossClient.putObject(systemPropertiesConfig.getAliYunOss().getBucketName(), fileName, file);
    }

    public void deleteFile(String key) {
        ossClient.deleteObject(systemPropertiesConfig.getAliYunOss().getBucketName(), key);
    }

    public JSONObject uploadFiles(MultipartFile multipartFile) throws IOException {
        String extensionName = FileUtil.getExtensionName(multipartFile.getOriginalFilename());
        String fileRealName = FileUtil.getFileNameNoEx(multipartFile.getOriginalFilename()) + "." + extensionName;
        String filename = sequence.nextString() + "." + extensionName;
        DateTime dateTime = DateTime.now();
        String url = "";
        if (!systemPropertiesConfig.getAliYunOss().isUseOss()) {
            String time = dateTime.toString("YYYYMMdd");
            String locationFilePath = systemPropertiesConfig.getAliYunOss().getFilesUploadPath() + time + "/";
            File localFile = new File(locationFilePath, filename);
            File localFilePath = new File(locationFilePath);
            if (!localFilePath.exists()) {
                localFilePath.mkdir();
            }
            multipartFile.transferTo(localFile);
            url = systemPropertiesConfig.getAliYunOss().getFilesUploadPathBaseUrl() + time + "/" + filename;
        } else {
            String baseUrl = systemPropertiesConfig.getAliYunOss().getBaseFilePrefix() + dateTime.toString("YYYYMMdd") + "/" + filename;
            uploadFile(baseUrl, multipartFile.getBytes());
            url = systemPropertiesConfig.getAliYunOss().getHost() + baseUrl;
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("url", url);
        jsonObject.put("fileName", fileRealName);
        return jsonObject;
    }
}
